<template>
  <div>
    <el-card class="box-card">
      <div slot="header" style="text-align:left">
        <el-form :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item label="商品">
            <el-input v-model="searchForm.productName" placeholder="商品名"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="backList">返回</el-button>
          </el-form-item>
           <el-form-item>
            <el-button type="primary" @click="add">添加</el-button>
          </el-form-item>
        </el-form>
       
      </div>
      <el-table :data="products" border style="width: 100%">
        <el-table-column prop="productImage" label="图片" style="width: 20%">
              <template slot-scope="scope">
                  <img width=50 height=50 v-if="scope.row.productImage && scope.row.productImage.length>0" :src="baseImage+scope.row.productImage">
                  <span v-else>无图</span>
              </template>
          </el-table-column>
        <el-table-column prop="productName" label="商品名" style="width: 20%"></el-table-column>
         <el-table-column prop="salePrice" label="原销售价" style="width: 20%"></el-table-column>
         <el-table-column prop="discount" label="打折" style="width: 50%">
              <template slot-scope="scope">
                  <span v-if="scope.row.discount==1">不打折</span>
                  <span style="color:red" v-else> {{scope.row.discount*10}}折</span>
              </template>
         </el-table-column>
         <el-table-column prop="discountPrice" label="折后价" style="width: 50%"></el-table-column>
    
      </el-table>
      <div class="block" style="margin-top:10px;">
        <el-pagination
          background
          @current-change="handleCurrentChange"
          :current-page.sync="pagenow"
          :page-size="pagesize"
          layout="total, prev, pager, next"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>

     <el-dialog :visible.sync="dialog"  width="40%" center style="text-align: center" >
          <el-transfer :titles="['源商品', '选中商品']" v-model="chooseProducts" :data="transData"  @change="handleChange" ></el-transfer>
    </el-dialog>

  </div>
</template>

<script>
import { activityProductList ,productSearch,activityProductBind} from "@/libs/api.js";
export default {
  methods: {
   backList(){
      this.$router.push({
        path: "/activity/list"
      });
    },

    /**选中商品变化 */
    handleChange(value, direction, movedKeys) {
         var param ={"id":this.$route.query.id};
         let products =[];
         if(this.transData && this.transData.length>0){
            this.transData.forEach((item, index) => {
              for(var i=0;i<value.length;i++){
                  if (item.id == value[i]) {
                    products.push(this.transData[index]);
                    break;
                  }
              }
            
            })
         }
         param.products=products;
        activityProductBind(param).then(res => {
          this.pagenow = 1;
          this.loadPage();
        });
    },
    /**
     * 分页
     */
    handleCurrentChange(p) {
      this.loadPage(p);
    },
    onSubmit() {
      this.pagenow = 1;
      this.loadPage();
    },
    loadPage() {
      var param = { pagesize: 10, pagenow: this.pagenow,"activityId":this.$route.query.id,"name":this.searchForm.productName};
      activityProductList(param).then(res => {
        this.products = res.rows;
        this.total = res.total;
      });
    },
    add(){
     this.dialog=true;
    },
  },
  mounted() {
    
    this.loadPage();
    var param ={"pagenow":"0"};
    productSearch(param).then(res => {
      let products = res.rows;
      if(products && products.length>0){
          this.transData=[];
          for(var i=0;i<products.length;i++){
            products[i].key=products[i].id;
            products[i].label=products[i].name;
            delete products[i].detailpc;
            delete products[i].detailh5;
            this.transData.push(products[i]);
          }
      }
    });
     this.baseImage=this.$http.baseImage;
  },

  data() {
    return {
      dialog:false,
      products: [],
      transData:[],
      chooseProducts:[],
      total: 0,
      pagenow: 1,
      pagesize: 10,
      baseImage:"",
      searchForm: {
        productName: ""
      }
    };
  }
};
</script>